let express = require('express');
let Router = express.Router();
module.exports = Router;

let query = require('./db');

/* 
    编写数据分类的数据接口
    [
        {
            type_name:'其他',
            sub_list:[
                {},{},{}
            ]
        },
        {
            type_name:'其他',
            sub_list:[
                {},{},{}
            ]
        }
    ]
    1.先查询level=1的所有数据（一级分类）
    2.根据一级分类的id去查询一级分类的子类
        一级分类的id === 子类的.parent_id
*/

// http://localhost:9090/api/goods/dataType
Router.get('/dataType', async (req, res) => {
    let data = [];
    let level1 = await query(`select * from goodsType where level=1;`);
   
    let sql = '';
    level1.forEach( (item) => {
        data.push(item);
        sql += `select * from goodsType where parent_id =  ${item.id};`
    });

    let sub_list = await query(sql);

    // 把子类数据处理放入分类的sub_list中
    // sub_list = [[],[],[],[]]
    sub_list.forEach((item,index) => {
        data[index].sub_list = [...item];
    })

    res.send({
        code: 200,
        msg: '请求成',
        data,
    })
})

/* 
    实现首页的商品列表的数据接口
    http://localhost:9091/api/goods/getData?page=1&limit=20
    这个数据接口应该有两个参数
        page:数字 页数，选填，默认值为1 
        limit:数字，每一页有多少条数据，选填，默认值为20
*/
Router.get('/getData',async (req, res) => {
    let { page, limit } = req.query;
    page = page*1 || 1;
    limit = limit * 1 || 20;
    // page = 1,limit = 20, limit 0,20
    // page = 2,limit = 20, limit 20,20
    let index = (page - 1) * limit;
    
    let data = await query(`select * from goods limit ${index},${limit};`)
    res.send({
        code: 200,
        msg: '查询成功',
        data,
        next_page:page+1,
    })
})

// 根据id查商品
// http://localhost:9091/api/goods/getShop?cate_id=10&two_menu=37
Router.get('/getShop',async(req,res)=>{
    let {cate_id,two_menu,page,limit}=req.query
    page = page*1 || 1;
    limit = limit * 1 || 8;
    let index = (page - 1) * limit;
    if(!two_menu){
        console.log('没有');
        let data=await query(`SELECT * FROM goods WHERE cat_one_id=${cate_id} limit ${index},${limit};`)
        let data1=await query(`SELECT * FROM goods WHERE cat_one_id=${cate_id} `)
        res.send({
            code: 200,
            msg: '查询成功',
            data,
            data1
        })
    }else{
        console.log('有');
        let data=await query(`SELECT * FROM goods WHERE cat_one_id=${cate_id} AND cat_two_id='${two_menu}' limit ${index},${limit};`)
        let data1=await query(`SELECT * FROM goods WHERE cat_one_id=${cate_id} AND cat_two_id='${two_menu}' `)
        res.send({
            code: 200,
            msg: '查询成功',
            data,
            data1
        })
    }
   
})
// 详情
Router.get('/details/:goods_id',async (req, res) => {
    let { goods_id } = req.params;
    try {
        let data = await query(`select * from goods where goods_id=${goods_id};`)
        if (data.length == 0) {
            res.send({
                code: 400,
                msg:'查询数据错误'
            })
            return 
        }
        res.send({
            code: 200,
            msg: '查询成功',
            ...data[0]
        })
    } catch (error) {
       res.send({
            code: 400,
            msg:error
        }) 
    }
})